﻿// Module:             Games Programming
// Assignment1:        Platformer Game
// Add game feature:   Laser door trap
// Student Number:     S10095741D
// Student Name:       Ng Cheong Sian

//////////////////////////////////////////////////////////////////////////


using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Game.TrapScripts
{
    class LaserDoor : ITrap
    {

        private readonly Texture2D _texture;
        private readonly Texture2D _textureBg;
        private Vector2 _position;
        private readonly float _timeOn;
        private readonly float _timeOff;
        private float _newTime;
        
        private bool _isActive = true;

        public LaserDoor(Vector2 position, float timeon, float timeoff, Texture2D tex, Texture2D bg)
        {
            _texture = tex;
            _textureBg = bg;
            _position = position;
            _timeOn = timeon;
            _timeOff = timeoff;
        }

        public void Update(GameTime gameTime)
        {
            if(_isActive && gameTime.TotalGameTime.TotalSeconds > _newTime)
            {
                _newTime = (float) (gameTime.TotalGameTime.TotalSeconds + _timeOff);
                _isActive = false;
            }
            else if (!_isActive && gameTime.TotalGameTime.TotalSeconds > _newTime)
            {
                _newTime = (float) (gameTime.TotalGameTime.TotalSeconds + _timeOn);
                _isActive = true;
            }
        }

        public Rectangle GetBoundingRect()
        {
            var rect = new Rectangle((int)_position.X, (int)_position.Y, _texture.Width, _texture.Height);
            return rect;
        }

        public bool IsActive { get { return _isActive; } }
        public TrapType Type { get {return TrapType.LaserDoor;} }


        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(_textureBg, _position, Color.White);
            if(_isActive)
                spriteBatch.Draw(_texture, _position, Color.White);
        }
    }
}
